﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class ChairDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// Load all Chairs by RoomId
        /// </summary>
        /// <param name="roomId">RoomId</param>
        /// <returns>List of Chair</returns>
        /// <author>VuongVT</author>
        public List<Chair> LoadChairs(int roomId)
        {
            List<Chair> listChair = new List<Chair>();
            try
            {
                var resultQuery = from chairs in _entities.Chairs
                                  where chairs.RoomId == roomId
                                        && chairs.IsDeleted == false
                                  select chairs;
                if (resultQuery != null)
                {
                    listChair = resultQuery.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return listChair;
        }

        /// <summary>
        /// Get Chair by ChairId
        /// </summary>
        /// <param name="id">ChairId</param>
        /// <returns>Chair</returns>
        /// <author>VuongVT</author>
        public Chair GetChairById(int id)
        {
            Chair chairEnt = new Chair();
            try
            {
                chairEnt = (from chair in _entities.Chairs
                           where chair.ChairId == id
                                 && chair.IsDeleted == false
                           select chair).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return chairEnt;
        }

        /// <summary>
        /// Update status Booked for chair
        /// </summary>
        /// <returns>1: success -- 0: fail</returns>
        /// <param name="id">ChairId</param>
        /// <author>VuongVT</author>
        public int UpdateChair(int id)
        {
            try
            {
                Chair chair = new Chair();
                chair = GetChairById(id);
                chair.IsBooked = true;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception)
            {
                return 0;
            }

        }

    }
}